﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;

namespace MeasurementTools
{
    public delegate void DelegateUpdatePositionOfAllPropertyWindow(int senderID, Thickness propertyWindowMargin);
    public delegate void DelegateSetPositionPreviewEnable(bool flag);
    public delegate void DelegateSetDisplayFontSize(int type);      //0 normal, 1 larger, 2 largest
    
    public enum UNIT_TYPE { UT_PIXEL, UT_NANO, UT_MICRO, UT_MM, UT_MIL, UT_CENTIMETER, UT_INCH, UT_METER };


    public class MeasurementToolsManager  
    {
        int m_controlIDs = 1;
        Point m_backgroundImageSize;
        private static MeasurementToolsManager m_instance = null;
        List<UserControl> m_listControls = new List<UserControl>();
        DelegateUpdatePositionOfAllPropertyWindow m_delegateUpdatePositionOfAllPropertyWindow;
        DelegateSetPositionPreviewEnable m_delegateSetPositionPreviewEnable;
        DelegateSetDisplayFontSize m_delegateSetDisplayFontSize;

        protected MeasurementToolsManager()
        {
            installDelegates();
        }

        public static MeasurementToolsManager New()
        {
            if (m_instance == null)
                m_instance = new MeasurementToolsManager();

            return m_instance;
        } 

        public UserControl1 CreateCircleControl(string name)
        {
            UserControl1 usercontrol1 = new UserControl1();

            usercontrol1.Name = name;
            usercontrol1.ID = m_controlIDs++;
            usercontrol1.ImageSize = m_backgroundImageSize;
            usercontrol1.m_delegateUpdatePositionOfAllPropertyWindow = m_delegateUpdatePositionOfAllPropertyWindow;
            usercontrol1.MSettingItem.M_delegateSetPositionPreviewEnable = m_delegateSetPositionPreviewEnable;
            usercontrol1.MSettingItem.M_delegateSetDisplayFontSize = m_delegateSetDisplayFontSize;

            m_listControls.Add(usercontrol1);

            return usercontrol1;
        }

        public void DestroyCircleControl(UserControl1 control)
        {
            foreach (UIElement ue in m_listControls)
            {
                UserControl1 ue1 = ue as UserControl1;
                if (ue1 != null)
                {
                    if (control == ue1)
                    {
                        m_listControls.Remove(control);
                        break;
                    }
                }
            }
        }

        public void SetCalibration(UNIT_TYPE type, double value)
        {
            foreach (UIElement ue in m_listControls)
            {
                UserControl1 ue1 = ue as UserControl1;
                if (ue1 != null)
                {
                    ue1.SetCalibration(type, value);
                }
            }
        }

        public void SetDisplayFontSize(int type)
        {
            foreach (UIElement ue in m_listControls)
            {
                UserControl1 ue1 = ue as UserControl1;
                if (ue1 != null)
                {
                    ue1.DisplayFontSize = type;
                }
            }
        }

        public void SetBackgroundImageSize(Point size)
        {
            m_backgroundImageSize = size;

            foreach (UIElement ue in m_listControls)
            {
                UserControl1 ue1 = ue as UserControl1;
                if (ue1 != null)
                {
                    ue1.ImageSize = size;
                }
            }
        }

        public void SetPositionPreviewEnable(bool flag)
        {
            foreach (UIElement ue in m_listControls)
            {
                UserControl1 ue1 = ue as UserControl1;
                if (ue1 != null)
                {
                    ue1.SetPositionPreviewEnable(flag);
                }
            }
        }

        
        public void UnselectAllControls()
        {
            foreach (UIElement ue in m_listControls)
            {
                UserControl1 ue1 = ue as UserControl1;
                if (ue1 != null)
                {
                    ue1.IsSelected = false;
                }
            }
        }


        private void installDelegates()
        {
            m_delegateSetPositionPreviewEnable = delegate(bool flag)
            {
                SetPositionPreviewEnable(flag);
            };

            m_delegateSetDisplayFontSize = delegate(int type)
            {
                SetDisplayFontSize(type);
            };

            m_delegateUpdatePositionOfAllPropertyWindow = delegate(int senderID, Thickness propertyWindowMargin)
            {
                foreach (UIElement ue in m_listControls)
                {
                    UserControl1 ue1 = ue as UserControl1;
                    if (ue1 != null)
                    {
                        if (senderID != ue1.ID)
                        {
                            ue1.PropertyWindowMargin = propertyWindowMargin;
                        }
                    }
                }
            };
        }
    }
}
